from common.Ulitys import ForumDriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ForumRegister:
    driver=""
    url=""
    def __init__(self):
        self.driver = ForumDriver.driver
        self.url = "http://127.0.0.1:9580/sign-up.html"
        self.driver.get(self.url)
        
    def RegisterSuccTest(self):
        # 清理输入框和 取消勾选协议
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        time.sleep(1)
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        
        
        # 输入用户名、昵称、密码、确认密码，且密码与确认密码一致，勾选协议，点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("testkySuc1123")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("testkySuc1123")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        # 勾选同意协议
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        # 点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 跳转到登录页面
        time.sleep(2)
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2").text
        assert text=="用户登录"
        
    def RegisterFailTest(self):
        # 清理输入框和 取消勾选协议
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        
        # 1.不输入用户名, 输入昵称、密码、确认密码，勾选协议，点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        time.sleep(1)
        ForumDriver.getScreenShot() # 截图
        text=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(2) > div").text
        assert text=="用户名不能为空"
        
        # 2.不输入昵称, 输入用户名、密码、确认密码，勾选协议，点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        time.sleep(1)
        ForumDriver.getScreenShot() # 截图
        text=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > div").text
        assert text=="昵称不能为空"
        
        # 3.不输入密码, 输入用户名、昵称、确认密码，勾选协议，点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        ForumDriver.getScreenShot() # 截图
        text=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(4) > div > div").text
        assert text=="密码不能为空"
        
        # 4.不输入确认密码, 输入用户名、昵称、密码，勾选协议，点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        
        time.sleep(1)
        ForumDriver.getScreenShot() # 截图
        text=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div").text
        assert text=="请检查确认密码"
        
        # 5.输入用户名、昵称、密码、确认密码，确认密码与密码不一致
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        ForumDriver.getScreenShot() # 截图  
        text=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div").text
        assert text=="请检查确认密码"
        
        # 6.输入用户名、昵称、密码、确认密码，不勾选协议，点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        ForumDriver.getScreenShot() # 截图
        ## 检查协议是否勾选
        checkbox = self.driver.find_element(By.CSS_SELECTOR,"#policy")
        assert checkbox.is_selected() == False
        time.sleep(1)
        
        # 7. 用户名已存在
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()
        checkbox=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
            
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("testky")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        # print("复选框状态:", checkbox.is_selected())  # 打印当前状态
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()            
        # print("复选框状态:", checkbox.is_selected())  # 打印当前状态
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 显示等待
        WebDriverWait(self.driver,10).until(
            EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2"))
        )
        ForumDriver.getScreenShot() # 截图
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="警告"
        

